package cn.lls.service.impl;

import cn.lls.base.exception.LlsException;
import cn.lls.base.result.ResultCode;
import cn.lls.dao.RoleDao;
import cn.lls.entity.sys.EmployeeRoleEntity;
import cn.lls.entity.sys.RoleEntity;
import cn.lls.entity.sys.RolePermissionEntity;
import cn.lls.req.company.ConditionSearchRoleReq;
import cn.lls.req.company.RoleUpsertReq;
import cn.lls.service.IEmployeeRoleService;
import cn.lls.service.IRolePermissionService;
import cn.lls.service.IRoleService;
import cn.lls.vo.company.CompanyAllInfo;
import cn.lls.vo.company.RoleInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 划水艺术家
 * @since 2022-11-15
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleDao, RoleEntity> implements IRoleService {

    @Autowired
    private IEmployeeRoleService employeeRoleService;

    @Autowired
    private IRolePermissionService rolePermissionService;

    @Override
    public  void addRole(RoleUpsertReq roleUpsertReq){
        RoleEntity roleEntity = new RoleEntity();
        BeanUtils.copyProperties(roleUpsertReq, roleEntity);
        // 插入角色基本信息
        this.baseMapper.insert(roleEntity);
        List<RolePermissionEntity> rolePermissionEntities = roleUpsertReq.getPermissionIds().stream().map((id)->{
            RolePermissionEntity rolePermissionEntity = new RolePermissionEntity();
            rolePermissionEntity.setRoleId(roleEntity.getRoleId());
            rolePermissionEntity.setPermissionId(id);
            return rolePermissionEntity;
        }).collect(Collectors.toList());
        // 插入权限
        rolePermissionService.saveBatch(rolePermissionEntities);
    }


    @Override
    public  void updateRole(RoleUpsertReq roleUpsertReq){
        RoleEntity roleEntity = new RoleEntity();
        BeanUtils.copyProperties(roleUpsertReq, roleEntity);
        LambdaUpdateWrapper<RoleEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(RoleEntity::getRoleId, roleEntity.getRoleId());
        // 更改角色基本信息
        this.baseMapper.update(roleEntity, updateWrapper);

        // 删除原有权限多余部分
        rolePermissionService.deleteRP(roleUpsertReq.getRoleId(), roleUpsertReq.getPermissionIds());

        // 新增原有权限没有的部门
        // 首先获取数据库中该角色含有的权限 id
        List<Integer> nowPermissionIds = rolePermissionService.getPermissionIdByRoleId(roleUpsertReq.getRoleId());
        // 获取差集
        List<Integer> reducePermissionIds = roleUpsertReq.getPermissionIds().stream().filter(item ->
                !nowPermissionIds.contains(item)
        ).collect(Collectors.toList());
        // 插入差集中的元素
        List<RolePermissionEntity> rolePermissionEntities = reducePermissionIds.stream().map((id)->{
            RolePermissionEntity rolePermissionEntity = new RolePermissionEntity();
            rolePermissionEntity.setRoleId(roleEntity.getRoleId());
            rolePermissionEntity.setPermissionId(id);
            return rolePermissionEntity;
        }).collect(Collectors.toList());
        // 插入权限
        rolePermissionService.saveBatch(rolePermissionEntities);
    }



    @Override
    public void delRole(Long roleId) {
        LambdaQueryWrapper<EmployeeRoleEntity> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(EmployeeRoleEntity::getRoleId, roleId);
        List<EmployeeRoleEntity> list = employeeRoleService.list(queryWrapper);
        if (list != null && list.size() > 0)
            throw new LlsException(ResultCode.EXE_ERROR, "当前角色含员工，无法删除");
        try{
            int delete = this.baseMapper.deleteById(roleId);
            if (delete == 0)
                throw new LlsException(ResultCode.DATA_ERROR, "数据异常");
        } catch (Exception e){
            throw new LlsException(ResultCode.DATA_ERROR, "数据异常");
        }
    }

    @Override
    public IPage<RoleInfo> getRoleInfoByPageAndCondition(ConditionSearchRoleReq conditionSearchRoleReq){
        IPage<CompanyAllInfo> page = new Page<>(conditionSearchRoleReq.getCurrentPage(), conditionSearchRoleReq.getPageSize());
        return this.baseMapper.getRoleInfoByPageAndCondition(page, conditionSearchRoleReq);
    }
}
